<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/27/027
 * Time: 15:41
 */

namespace App\Http\Controllers\Recharge;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Session;
use Config;
use App\Http\Service\Recharge\RechargeService;

require_once 'phpqrcode.php';
use QRcode;

use Illuminate\Support\Facades\Redis;

class IndexController extends Controller
{
    public function recharge()
    {
        return view("Index.voucher_center");
    }


    /**
     * @param Request $request
     * @return mixed
     * 支付宝充值
     */
    public function aliRecharge(Request $request)
    {
        $where = $request->except('_token');
        $where['token'] = Session::get(config::get('custom.setting.MEMBER_TOKEN'));
        $where['money'] = (int)$request->input('money');
        $where['pay_type'] = $request->input('pay_type');
        $where['order_no'] = self::createOrderId();

        $recharge = new RechargeService();
        $data = $recharge->AliRecharge($where);
        if($data['error_code'] == 0){
            return $data['data'];
        }else{
            return $data;
        }

    }

    /**
     * @param Request $request
     * 微信扫码支付
     */
    public function WxRecharge(Request $request)
    {
        $where = [];
        $where['money'] = (int)$request->input('money');
        $where['pay_type'] = $request->input('pay_type');
        $where['token'] = Session::get(config::get('custom.setting.MEMBER_TOKEN'));
        $where['order_no'] = self::createOrderId();
        $recharge = new RechargeService();
        $data = $recharge->WxRecharge($where);
        if($data['error_code'] == 0){
            return QRcode::png($data['data']);
        }else{
            return $data;
        }



    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 检测充值订单的状态
     */
    public function CheckRechargeStatus(Request $request)
    {
        $where = [];
        $where['token'] = Session::get(config::get('custom.setting.MEMBER_TOKEN'));
        $recharge = new RechargeService();
        $data = $recharge->CheckRechargeStatus($where);
        return $data;
    }


    //可以指定前缀   生成用户ID
    static function createOrderId($prefix = "")
    {
        $str = md5(uniqid(mt_rand(), true));
        $uuid  = substr($str,0,8) . '-';
        $uuid .= substr($str,8,4) . '-';
        $uuid .= substr($str,12,4) . '-';
//        $uuid .= substr($str,16,4) . '-';
        $uuid .= substr($str,20,12);
        return $prefix . $uuid;
    }
}